/**
 * OSAmI: Open Source Ambient Intelligence
 * A simple publish/subscribe/dispatch API (based on multicast communications) for constrained devices.
 * Copyright (C) Lionel Debroux 2006 (as a TER/Magistere M1 student in the LSR-IMAG-ADELE team, for a demo in the H-Omega project)
 * Copyright (C) Lionel Debroux 2010 (as an engineer in the LCIS-INPG-CTSYS team, for a pre-demo in the FR part of the OSAMI project)
 * Contact: osami-general@osami-commons.org
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation;
 * version 2.1 of the License.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Suite 500 Boston, MA 02110-1335 USA
 */

package org.osami.gateway.sensor.internal.multicast;

import java.net.InetAddress;

import org.osami.gateway.sensor.internal.message.parser.Constants;
import org.osami.gateway.sensor.protocol.Message;
import org.osami.gateway.sensor.protocol.IPayload;

/**
 * This class generates messages.
 * The work is done by the constructor.
 * Keep MulticastInboundMessage and MulticastOutboundMessage in sync !
 */
public class MulticastOutboundMessage implements Message {

	private InetAddress from;
	private int portnr;
	private InetAddress to;
	private String payload;
	private String type;

	/**
	 * Generate message from given originator address, to address, port number and payload.
	 */
	public MulticastOutboundMessage(String from, int portnr, String to, String payload, IPayload.Type type) throws Exception {

		//this.from = InetAddress.getByName(from);
		this.portnr = portnr;
		this.to = InetAddress.getByName(to);
		this.payload = payload;

		if (type == IPayload.Type.HELLO) {
			this.type = Constants.DEVICE_HELLO_PREFIX;
		}
		else if (type == IPayload.Type.PROBE) {
			this.type = Constants.DEVICE_PROBE_PREFIX;
		}
		else if (type == IPayload.Type.GET_METADATA) {
			this.type = Constants.DEVICE_GETMETADATA_PREFIX;
		}
		else if (type == IPayload.Type.METADATA) {
			this.type = Constants.DEVICE_METADATA_PREFIX;
		}
		else if (type == IPayload.Type.BYE) {
			this.type = Constants.DEVICE_BYE_PREFIX;
		}
		else /*if (type == Type.EVENT)*/ {
			this.type = Constants.DEVICE_EVENT_PREFIX;
		}
	}

	// Getters, setters.
	public String toString() {
		if (!payload.equals("")) {
			return type + to.toString().replaceAll("/", "") + Constants.DEVICE_FIELD_SEPARATOR + payload + Constants.DEVICE_FIELD_SEPARATOR;
		}
		else {
			return type + to.toString().replaceAll("/", "") + Constants.DEVICE_FIELD_SEPARATOR;
		}
	}

	public String getPayload() {
		return payload;
	}

	public int getToPortNumber() {
		return portnr;
	}

	public InetAddress getToAddress() {
		return to;
	}

	public InetAddress getFromAddress() {
		return from;
	}

	/*public void setMessage(String message) {
		this.message = message;
	}*/

	/*public void setPortnr(int portnr) {
		this.portnr = portnr;
	}*/

	/*public void setTo(InetAddress to) {
		this.to = to;
	}*/
}
